package ar.edu.uba.fi.jmit.integrationtest;

import org.apache.log4j.Logger;

/**
 * Clase base para todos los tests de integracion
 * @author Tomas
 *
 */
public abstract class IntegrationTest {
	
	protected Logger logger = Logger.getLogger(this.getClass());
	
	private String testName;
	
	protected boolean stopping = false;
	
	public IntegrationTest(String testName) {
		this.testName=testName;
	}
	
	public IntegrationTest() {
		this.testName = this.getClass().getSimpleName();
	}

	public void stop() {
		logger.info("Stopping Test " + testName);
		stopping = true;
		if(stopTest()) {
			logger.info("[OK] \tTest " + testName + " stopped OK");
		} else {
			logger.info("[FAIL] \tFail to stop " + testName);
		}
		
	}

	protected abstract void runTest();

	public void start() {
		logger.info("Starting " + testName + "...");
		runTest();
		logger.info(testName + " started");
	}

	public boolean isStopping() {
		return stopping;
	}
	
	protected boolean stopTest() {
		stopping = true;
		while(!allThreadsStopped());
		return true;
	}

	protected abstract boolean allThreadsStopped();

}
